#include"headFile/a_connect.h"

a_connect::a_connect(int fd){
        this->fd = fd;
        rio_readinitb(&this->rio,fd);
}
int a_connect::get_request_row(){
    char buf[100];
    int n = rio_readlineb(&this->rio,buf,Max);
    if(n==0) return n;
    cout<<"请求行:\n"<<buf<<endl;
    sscanf(buf,"%s %s %s",this->method,this->url_all,this->version);
    cout<<"1"<<endl;
    return n;
}
void a_connect::get_url_type(){
    cout<<"1"<<endl;
    sscanf((this->url_all)+1,"%c%s",&this->url_type,this->url);
    cout<<"type:"<<this->url_type<<endl;
}
void a_connect::get_request_head(){
    char buf[Max];
    cout<<"请求头:"<<endl;
    rio_readlineb(&this->rio,buf,Max);
    cout<<buf;
    while(strcmp(buf,"\r\n")){ //strcmp：比较字符串  相同返回0
        rio_readlineb(&this->rio,buf,1000);
        cout<<buf;
    }
}
void a_connect::respose(int static_fd,int status,char* hand_message){
    char buf[Max],body[Max];
    int n = read(static_fd,body,Max);//获取响应体
    cout<<"响应行:"<<endl;
    sprintf(buf,"%s %d %s\r\n",this->version,&status,hand_message); //响应行:
    cout<<buf;
    rio_writen(fd,buf,strlen(buf));
    cout<<"响应头:"<<endl;
    sprintf(buf,"Content-type: text/html\r\n");
    cout<<buf<<endl;
    rio_writen(fd,buf,strlen(buf));
    sprintf(buf,"Content-length:%d\r\n\r\n",n);
    cout<<buf;
    rio_writen(fd,buf,strlen(buf));
    cout<<"响应体:"<<endl;
    cout<<body<<endl;
    rio_writen(fd,body,n);
}